package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.FoodDao;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 啊滨
 * @date 2023/2/14 13:29
 */
public class FoodDaoImpl implements FoodDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception {
        List<Food> list=new ArrayList<>();
        String sql="SELECT * FROM food WHERE businessId=? GROUP BY foodId";
        try {
            //获取链接
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setInt(1,businessId);
            //执行查询
            rs=pst.executeQuery();
            //遍历结果集
            while (rs.next()){
                Food food=new Food();
                food.setBusinessId(rs.getInt("businessId"));
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));
                list.add(food);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}
